<?php
	function imagetrim($im, $bg){
	    $imw = imagesx($im);
	    $imh = imagesy($im);

	    $xmin = $imw;
	    $xmax = 0;

	    for ($iy=0; $iy<$imh; $iy++){
	        $first = true;
	        for ($ix=0; $ix<$imw; $ix++){
	            $ndx = imagecolorat($im, $ix, $iy);
	            if ($ndx != $bg){
	                if ($xmin > $ix){ $xmin = $ix; }
	                if ($xmax < $ix){ $xmax = $ix; }
	                if (!isset($ymin)){ $ymin = $iy; }
	                $ymax = $iy;
	                if ($first){ $ix = $xmax; $first = false; }
	            }
	        }
	    }

	    $imw = 1 + $xmax - $xmin;
	    $im2 = imagecreate($imw, $imh);

		$bg = ImageColorAllocate($im2, hexdec(substr($_GET['b'], 0, 2)), hexdec(substr($_GET['b'], 2, 2)), hexdec(substr($_GET['b'], 4, 2)));
		//$trans = ImageColorTransparent($im2, $bg);
		ImageFill($im2, 0, 0, $bg);
	    imagecopy($im2, $im, 0, round(($imh-$ymin)/2), $xmin, $ymin, $imw, $imh);

	    return $im2;
	}

	unset($_GET['action']);
	unset($_GET['disc']);
	
	$t1 = explode("?", $_SERVER['REQUEST_URI']);
	if (isset($t1[1])) $t1 = explode(";", $t1[1]);
	else $t1 = NULL;
	if ($t1 != NULL){
		foreach($t1 as $value){
			$t2 = explode("=", $value);
			if (count($t2) == 2) $_GET[$t2[0]] = $t2[1];
		}
	}
	
	$uni_nam = md5(serialize($_GET));
	if (file_exists($setupdata['binary_path']."/ttf/".$uni_nam.".gif")){
		header("Content-type: image/gif");
		header("Content-Length: ".filesize($setupdata['binary_path']."/ttf/".$uni_nam.".gif"));
		$handle = fopen($setupdata['binary_path']."/ttf/".$uni_nam.".gif", "rb");
		while (ob_get_level()) ob_end_clean();
		if ($handle !== false){
			while (!feof($handle)) echo fread($handle, 1024);
			fclose($handle);
		}
		$mysqlclass->close_db();
		include $setupdata['core_dir']."/includes/die.php";
	}
		
	if (
		isset($_GET['h']) && (intval($_GET['h']) > 0) && 
		isset($_GET['f']) && (strlen($_GET['f']) > 0) && isset($_GET['b']) && (strlen($_GET['b']) == 6) &&
		isset($_GET['c']) && (strlen($_GET['c']) == 6) && isset($_GET['s']) && (strlen($_GET['s']) > 0) &&
		isset($_GET['z']) && (intval($_GET['z']) > 0)
		){
		if (!file_exists($setupdata['binary_path']."/ttf")){
			mkdir($setupdata['binary_path']."/ttf", 0777);
		}
		if (!file_exists($setupdata['binary_path']."/ttf/".$uni_nam.".gif")){
			if (!isset($_GET['w']) || (intval($_GET['w']) == 0)) $noW = true;
			else $noW = false;

			if ($noW == true) $_GET['w'] = 1500;
		
			$im = imagecreate($_GET['w'], $_GET['h']);
			$cl = ImageColorAllocate($im, hexdec(substr($_GET['c'], 0, 2)), hexdec(substr($_GET['c'], 2, 2)), hexdec(substr($_GET['c'], 4, 2)));
			$bg = ImageColorAllocate($im, hexdec(substr($_GET['b'], 0, 2)), hexdec(substr($_GET['b'], 2, 2)), hexdec(substr($_GET['b'], 4, 2)));
			$trans = ImageColorTransparent($im, $bg);
			ImageFill($im, 0, 0, $trans);
			if (strpos($_SERVER['DOCUMENT_ROOT'], ":") == false) $ttf_font = $setupdata['smarty_guest_template_dir']."font/".$_GET['f'];
			else $ttf_font = $_GET['f'];
			$y_pos = ($_GET['h']-$_GET['z'])/2+$_GET['z'];
			
			if (isset($_GET['p']) && ($_GET['p'] == "c")){
				$dimensions = imagettfbbox($_GET['z'], 0, $ttf_font, urldecode($_GET['s']));
				$cur_x = ($_GET['w'] - $dimensions[4] - $dimensions[0])/2;
			} elseif (isset($_GET['p']) && ($_GET['p'] == "r")){
				$dimensions = imagettfbbox($_GET['z'], 0, $ttf_font, urldecode($_GET['s']));
				$cur_x = $_GET['w'] - $dimensions[4] - $dimensions[0] - 1;
			} else {
				$cur_x = 1;
			}
	
			ImageTTFText($im, $_GET['z'], 0, $cur_x, $y_pos, $cl, $ttf_font, urldecode($_GET['s']));
			
			if ($noW == true) $im = imagetrim($im, $trans);
			
			imagegif($im, $setupdata['binary_path']."/ttf/".$uni_nam.".gif");
			ImageDestroy($im);
		}
		if (file_exists($setupdata['binary_path']."/ttf/".$uni_nam.".gif")){
			header("Content-type: image/gif");
			header("Content-Length: ".filesize($setupdata['binary_path']."/ttf/".$uni_nam.".gif"));
			$handle = fopen($setupdata['binary_path']."/ttf/".$uni_nam.".gif", "rb");
			while (ob_get_level()) ob_end_clean();
			if ($handle !== false){
				while (!feof($handle)) echo fread($handle, 1024);
				fclose($handle);
			}
		}
		$mysqlclass->close_db();
		include $setupdata['core_dir']."/includes/die.php";
	} else {
		$name = $setupdata['core_dir']."/design/img/no_image.jpg";
		$handle = fopen($name, 'rb');
		header("Last-Modified: ".gmdate("D, d M Y H:i:s", filemtime($name))." GMT");
		header("Content-Type: image/jpg");
		header("Content-Length: " . filesize($name));
		if ($handle !== false){
			while (!feof($handle)) echo fread($handle, 1024*1024);
			fclose($handle);
		}
		exit;
	}
	$mysqlclass->close_db();
	include $setupdata['core_dir']."/includes/die.php";
?>